<?php

namespace YmPhp\Admin\Http\Controllers\Member;

use YmPhp\Admin\Http\Repositories\Member\MemberList;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;

class MemberController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MemberList(), function (Grid $grid) {
            $grid->model()->orderByDesc('uid');
            $grid->column('uid')->sortable();
            $grid->column('avatar',__('member.avatar'))->display(function ($images){
                if ($images==''){
                    $img="<img data-action='preview-img' src='/static/images/default.png'  width='50px' height='50px'>";
                }else{
                    $img= "<img data-action='preview-img' src='$images' width='50px' height='50px'>";
                }
                return $img;
            })->width(50);
            $grid->column('username',__('member.username'))->width(200);
            $grid->column('mobile',__('member.mobile'))->width(100);
            $grid->column('money',__('member.money'))->width(100);
            $grid->column('credits',__('member.credits'))->width(100);
            $grid->column('regdate',__('member.reg_date'))->display(function (){
                return date('Y-m-d H:i:s',$this->regdate);
            })->width(100)->sortable();
            $grid->column('regip',__('member.regip'))->width(100);
            $grid->column('email',__('member.email'))->width(200);
            $grid->column('is_email',__('member.email_status'))->width(100)->using(['0'=>trans('admin.validate_no'),'1'=>trans('admin.validate_yes')])->label(['1' => 'primary','0' => 'danger']);
            $grid->column('is_avatar',__('member.avatar_status'))->width(100)->using(['0'=>trans('admin.validate_no'),'1'=>trans('admin.validate_yes')])->label(['1' => 'primary','0' => 'danger']);
            $grid->column('status',__('member.status'))->width(80)->using(['0'=>__('member.status0'),'1'=>__('member.status1'),'1'=>__('member.status2'),'3'=>__('member.status3')])->label(['1' => 'primary', '2' => 'danger', '3' => 'warning', '4' => 'default']);
            $grid->column('updated_at')->width(100)->sortable();

            $grid->quickSearch(['mobile', 'email', 'username', 'uid']);
            $grid->enableDialogCreate();
            $grid->disableViewButton();
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
//                $filter->panel();

                // 注意切换为panel布局方式时需要重新调整表单字段的宽度
                $filter->equal('id')->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MemberList(), function (Show $show) {
            $show->field('uid');
            $show->field('email');
            $show->field('username');
            $show->field('mobile');
            $show->field('password');
            $show->field('email_verified_at');
            $show->field('status');
            $show->field('remember_token');
            $show->field('regdate');
            $show->field('emailstatus');
            $show->field('avatarstatus');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MemberList(), function (Form $form) {
            $form->display('uid');
            $form->text('email');
            $form->text('username');
            $form->image('avatar')->autoUpload()->uniqueName()->saveFullUrl();
            $form->mobile('mobile');
            $form->text('password')->disable()->placeholder('密码不可编辑');
            $form->time('email_verified_at')->format('YYYY-MM-DD HH:mm:ss');
            $form->radio('status')->options(['1'=>trans('admin.validate_no'),'2'=>trans('admin.validate_yes')]);
            $form->text('remember_token')->disable();
            $form->text('regdate')->disable();
            $form->radio('emailstatus')->options(['1'=>trans('admin.validate_no'),'2'=>trans('admin.validate_yes')]);
            $form->radio('avatarstatus')->options(['1'=>trans('admin.validate_no'),'2'=>trans('admin.validate_yes')]);

        });
    }
}
